package com.bawei.module_home.ui

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.viewModels
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.alibaba.android.arouter.facade.annotation.Route
import com.bawei.lib_common.Router
import com.bawei.lib_common.base.state.Empty
import com.bawei.lib_common.base.state.Failed
import com.bawei.lib_common.base.state.Loading
import com.bawei.lib_common.base.state.Success
import com.bawei.lib_common.base.utils.ToastUtils
import com.bawei.module_home.R
import com.bawei.module_home.databinding.FragmentHomeBinding
import com.bawei.module_home.ui.adapter.HomeAdapter
import com.bawei.myclub.base.mvvm.view.BaseFragment
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.launch

@AndroidEntryPoint

class HomeFragment : BaseFragment<HomeViewModel, FragmentHomeBinding>() {
    override val mLayoutResId: Int = R.layout.fragment_home
    override val mViewModel: HomeViewModel by viewModels()

    private val adapter1 by lazy { HomeAdapter() }
    private val adapter2 by lazy { HomeAdapter() }
    private val adapter3 by lazy { HomeAdapter() }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        initViews()
        observeData()
        mBinding.itemTopHome.tvLogo.setOnClickListener(View.OnClickListener {
            requireActivity().finish()
        })
    }

    private fun initViews() {
        // 初始化第一个列表
        setupRecyclerView(mBinding.rvHome, mBinding.ivArrow, adapter1, 1)

        // 初始化第二个列表
        setupRecyclerView(mBinding.rvHome2, mBinding.ivArrow2, adapter2, 2)

        // 初始化第三个列表
        setupRecyclerView(mBinding.rvHome3, mBinding.ivArrow3, adapter3, 3)
    }

    private fun setupRecyclerView(
        recyclerView: RecyclerView,
        arrowView: View,
        adapter: HomeAdapter,
        categoryId: Int
    ) {
        recyclerView.layoutManager = LinearLayoutManager(requireContext(), LinearLayoutManager.HORIZONTAL, false)
        recyclerView.adapter = adapter

        arrowView.setOnClickListener {
            mViewModel.searchHomeById(categoryId)
            recyclerView.visibility = if (recyclerView.visibility == View.GONE) {
                View.VISIBLE
            } else {
                View.GONE
            }
        }
    }

    private fun observeData() {
        lifecycleScope.launch {
            mViewModel.homeList.collect { dataList ->
                if (dataList != null && dataList.isNotEmpty()) {
                    // 根据当前分类ID更新对应的适配器
                    when (mViewModel.currentCategoryId) {
                        1 -> adapter1.submitList(dataList)
                        2 -> adapter2.submitList(dataList)
                        3 -> adapter3.submitList(dataList)
                    }
                }
            }
        }
    }
}